
/**
 * Project Name:cats-web
 * File Name:UserController.java
 * Package Name:com.catsic.web.controllers
 * Date:2014年4月3日上午9:03:56
 * Copyright (c) 2014, oa.catsic.com All Rights Reserved.
 */
 
package com.catsic.web.controllers;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.catsic.base.BaseInfo;
import com.catsic.base.annotation.Log;
import com.catsic.base.controller.BaseController;
import com.catsic.core.conditon.UserCondition;
import com.catsic.core.entity.User;
import com.catsic.core.form.RoleForm;
import com.catsic.core.form.UserForm;
import com.catsic.core.organ.service.OrganService;
import com.catsic.core.status.UserStatus;
import com.catsic.core.user.service.UserService;

/**
 * ClassName: UserController
 * Description: 用户Controller
 * date: 2014年4月3日 上午9:03:56
 * author: yangyd
 */
@Controller
@RequestMapping("/user")
public class UserController extends BaseController{
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private OrganService organService;
	
    /**
     * 查询用户
	 * @author yangyd
	 * @param condition 
	 * @return
	 */
	@RequestMapping(value = "/query" , method = RequestMethod.GET)
	@ResponseBody
	public Map<String,Object> query(UserCondition condition,Pageable  pageable){
		Page<UserForm> page = userService.findAll(condition, pageable);
		Map<String,Object> map = new HashMap<String,Object>(); 
		map.put(CONTENT, page);
		map.put(RESULT, RESULT_SUCCESS);
		return map;
	}
	
	/**
	 * create:创建
	 * @author yangyd
	 * @param form
	 * @return
	 */
	@RequestMapping(value = "/create" , method = RequestMethod.POST)
	@ResponseBody
	@Log(module = "用户管理", operation = "添加用户")
	public Map<String,Object> create(UserForm form){
		form = userService.add(form);
		Map<String,Object> map = new HashMap<String,Object>(); 
		map.put(CONTENT, form);
		map.put(RESULT, RESULT_SUCCESS);
		return map;
	}
	
	/**
	 * update:更新
	 * @author yangyd
	 * @param form
	 * @return
	 */
	@RequestMapping(value = "/update" , method = RequestMethod.POST)
	@ResponseBody
	@Log(module = "用户管理", operation = "修改用户")
	public Map<String,Object> update(UserForm form){
		form = userService.update(form);
		Map<String,Object> map = new HashMap<String,Object>(); 
		map.put(CONTENT, form);
		map.put(RESULT, RESULT_SUCCESS);
		return map;
	}
	
	/**
	 * freeze:冻结
	 * @author yangyd
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/freeze")
	@ResponseBody
	@Log(module = "用户管理", operation = "冻结用户")
	public Map<String,Object> freeze(String id){
		userService.freeze(id);
		Map<String,Object> map = new HashMap<String,Object>(); 
		map.put(CONTENT, UserStatus.冻结);
		map.put(RESULT, RESULT_SUCCESS);
		return map;
	}
	
	/**
	 * cancel:注销
	 * @author yangyd
	 * @param  id
	 * @return
	 */
	@RequestMapping(value = "/cancel")
	@ResponseBody
	@Log(module = "用户管理", operation = "注销用户")
	public Map<String,Object> cancel(String id){
		userService.cancel(id);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put(CONTENT, UserStatus.注销);
		map.put(RESULT, RESULT_SUCCESS);
		return map;
	}
	
	/**
	 * active:激活
	 * @author yangyd
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/active")
	@ResponseBody
	@Log(module = "用户管理", operation = "激活用户")
	public Map<String,Object> active(String id){
		userService.activite(id);
		Map<String,Object> map = new HashMap<String,Object>(); 
		map.put(CONTENT, UserStatus.激活);
		map.put(RESULT, RESULT_SUCCESS);
		return map;
	}
	
	/**
	 * detail:详情
	 * @author yangyd
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/detail" , method = RequestMethod.GET)
	@ResponseBody
	public Map<String,Object> detail(String id){
		UserForm form =userService.findUserById(id);
		Map<String,Object> map = new HashMap<String,Object>(); 
		map.put(CONTENT, form);
		map.put(RESULT, RESULT_SUCCESS);
		return map;
	}
	
	/**
	 * changePassword:修改密码
	 * @author yangyd
	 * @return
	 */
	@RequestMapping(value = "/changePassword")
	@ResponseBody
	@Log(module = "用户管理", operation = "修改密码")
	public Map<String,Object> changePassword(String username,String oldPassword,String newPassword){
		userService.changePassword(username, oldPassword, newPassword);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put(RESULT, RESULT_SUCCESS);
		return map;
	}
	
	/**
	 * initPassword:初始密码
	 * @author yangyd
	 * @return
	 */
	@RequestMapping(value = "/initPassword")
	@ResponseBody
	@Log(module = "用户管理", operation = "初始化密码")
	public Map<String,Object> initPassword(String id,String newPassword){
		userService.initPassword(id,newPassword);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put(RESULT, RESULT_SUCCESS);
		return map;
	}
	
	/**
	 * initAllPassword:初始密码12345qwert
	 * @author yangyd
	 * @return
	 */
	@RequestMapping(value = "/initAllPassword")
	@ResponseBody
	@Log(module = "用户管理", operation = "初始化所有用户密码")
	public Map<String,Object> initAllPassword(String organCode,String newPassword){
		userService.initAllPassword(organCode,newPassword);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put(RESULT, RESULT_SUCCESS);
		return map;
	}
	
	/**
	 * bind:授权
	 * @author yangyd
	 * @param id 用户主键
	 * @param roleIds 角色主键数组
	 * @return
	 */
	@RequestMapping(value = "/bind" , method = RequestMethod.POST)
	@ResponseBody
	@Log(module = "用户管理", operation = "用户授权")
	public Map<String,Object> bind(String id,String roleIds[]){
		userService.bind(id,roleIds);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put(RESULT, RESULT_SUCCESS);
		return map;
	}
	
	
	/**
	 * findRoleById:通过用户ID获取用户对应的角色
	 * @author yangyd
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/findRoleById" , method = RequestMethod.GET)
	@ResponseBody
	public Map<String,Object> findRoleById(String id){
		List<RoleForm> listForm =userService.findRoleById(id);
		Map<String,Object> map = new HashMap<String,Object>(); 
		map.put(CONTENT, listForm);
		map.put(RESULT, RESULT_SUCCESS);
		return map;
	}
	
	/**
	 * findByUsername:通过用户名获取用户信息
	 * @author yangyd
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/findByUsername" , method = RequestMethod.GET)
	@ResponseBody
	public Map<String,Object> findByUsername(String username){
		User user =userService.findByUsername(username);
		Map<String,Object> map = new HashMap<String,Object>(); 
		map.put(CONTENT, user);
		map.put(RESULT, RESULT_SUCCESS);
		return map;
	}
	
	/**
	 * findUserInfo:PC端，手机端都在用
	 * @author yangyd
	 * @return
	 */
	@RequestMapping(value="/findUserInfo")
	@ResponseBody
	public Map<String,Object> findUserInfo(){
		BaseInfo baseInfo = getCurrentUser();
		UserForm userForm = userService.findUserById(baseInfo.getUserId());
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("user", userForm);
		map.put("result", RESULT_SUCCESS);
		return map;
	}
	
}
